const mongodb = require("mongodb");
const mongoClient = mongodb.MongoClient;
// 私有函数。
function _connect() {
    return new Promise((resolve, reject) => {
        mongoClient.connect("mongodb://127.0.0.1", { useUnifiedTopology: true }, function (err, client) {
            if (err) reject(err);
            else resolve(client.db("hungry"))
        })
    })

}
module.exports.insertOne = async function (collName, insertObj = {}) {
    const db = await _connect();
    return new Promise((resolve, reject) => {
        db.collection(collName).insertOne(insertObj, (err, results) => {
            if (err) reject(err);
            else resolve(results);
        })
    })

}

module.exports.count = async function (collName, whereObj = {}) {
    const db = await _connect();
    return new Promise((resolve, reject) => {
        db.collection(collName).countDocuments(whereObj).then(count => {
            resolve(count);
        })
    })
}
// 查找多条.  whereObj:搜索条件  skip:跳过的文档数  limit:显示的文档数  sortObj:排序对象
module.exports.find = async function (collName, { whereObj = {}, skip = 0, limit = 0, sortObj = {} }) {
    const db = await _connect();
    return new Promise((resolve, reject) => {
        db.collection(collName).find(whereObj).limit(limit).skip(skip).sort(sortObj).toArray(function (err, results) {
            if (err) reject(err)
            else resolve(results);
        })
    })
}
// 查找一条数据
module.exports.findOne = async function (collName, whereObj) {
    const db = await _connect();
    return new Promise((resolve, reject) => {
        db.collection(collName).findOne(whereObj, (err, results) => {
            if (err) {
                reject(err)
            } else {
                resolve(results)
            }
        })
    })

}
// 根据ID搜索一条信息
module.exports.findOneById = async function (collName, id) {
    const db = await _connect();
    return new Promise((resolve, reject) => {
        db.collection(collName).findOne({ _id: mongodb.ObjectId(id) }, (err, results) => {
            if (err)
                reject(err);
            else
                resolve(results);
        })
    })
}


/*
* 根据ID进行删除。*/
module.exports.deleteOneById = async function (collName, id) {
    const db = await _connect();
    return new Promise((resolve, reject) => {
        db.collection(collName).deleteOne({ _id: mongodb.ObjectId(id) }, function (err, results) {
            if (err) reject(err);
            else resolve(results);
        })
    })
}
// 根据条件进行修改一条
module.exports.updateOne = async function (collName, whereObj, upObj) {
    const db = await _connect();
    return new Promise((resolve, reject) => {
        db.collection(collName).updateOne(whereObj, upObj, function (err, results) {
            if (err) reject(err);
            else resolve(results);
        })
    })
}
/*
* 根据ID进行修改。*/
module.exports.updateOneById = async function (collName, id, upObj) {
    const db = await _connect();
    return new Promise((resolve, reject) => {
        db.collection(collName).updateOne({ _id: mongodb.ObjectId(id) }, upObj, function (err, results) {
            if (err) reject(err);
            else resolve(results);
        })
    })
}